package com.xdb.sqlparser.ast.statement;

import java.util.ArrayList;
import java.util.List;

import com.xdb.sqlparser.ast.SQLExpr;
import com.xdb.sqlparser.ast.AbstractSQLObjectImpl;
import com.xdb.sqlparser.visitor.SQLAstVisitor;
/**
 * SQL查询块，包含SELECT COL1,COL2 FROM TAB1 T1,TAB2 T2 WHERE T1.COL1=T2.COL1 GROUP BY T2.NAME的抽象 
 * @author woate
 *
 */
public class SQLSelectQueryBlock extends AbstractSQLObjectImpl implements SQLSelectQuery {

	protected int distionOption;
	protected final List<SQLSelectItem> selectList = new ArrayList<SQLSelectItem>();
	/**
	 * 查询所对应的FROM对象
	 */
	protected SQLTableSource from;
	/**
	 * 
	 */
	protected SQLExprTableSource into;
	protected SQLExpr where;
	/**
	 * GROUP BY 子句
	 */
	protected SQLSelectGroupByClause groupBy;
	protected boolean parenthesized = false;

	public SQLSelectQueryBlock() {

	}

	public SQLExprTableSource getInto() {
		return into;
	}

	public void setInto(SQLExpr into) {
		this.setInto(new SQLExprTableSource(into));
	}

	public void setInto(SQLExprTableSource into) {
		if (into != null) {
			into.setParent(this);
		}
		this.into = into;
	}

	public SQLSelectGroupByClause getGroupBy() {
		return this.groupBy;
	}

	public void setGroupBy(SQLSelectGroupByClause groupBy) {
		this.groupBy = groupBy;
	}

	public SQLExpr getWhere() {
		return this.where;
	}

	public void setWhere(SQLExpr where) {
		if (where != null) {
			where.setParent(this);
		}
		this.where = where;
	}

	public int getDistionOption() {
		return this.distionOption;
	}

	public void setDistionOption(int distionOption) {
		this.distionOption = distionOption;
	}

	public List<SQLSelectItem> getSelectList() {
		return this.selectList;
	}

	public void addSelectItem(SQLSelectItem item) {
		this.selectList.add(item);
		item.setParent(this);
	}
	/**
	 * 获取当前查询对象的FROM对象
	 * 
	 * @return
	 */
	public SQLTableSource getFrom() {
		return this.from;
	}

	public void setFrom(SQLTableSource from) {
		this.from = from;
	}

	public boolean isParenthesized() {
		return parenthesized;
	}

	public void setParenthesized(boolean parenthesized) {
		this.parenthesized = parenthesized;
	}

	@Override
	protected void accept0(SQLAstVisitor visitor) {
		if (visitor.visit(this)) {
			acceptChild(visitor, this.selectList);
			acceptChild(visitor, this.from);
			acceptChild(visitor, this.where);
			acceptChild(visitor, this.groupBy);
		}
		visitor.endVisit(this);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (Boolean.valueOf(parenthesized).hashCode());
		result = prime * result + distionOption;
		result = prime * result + ((from == null) ? 0 : from.hashCode());
		result = prime * result + ((groupBy == null) ? 0 : groupBy.hashCode());
		result = prime * result + ((into == null) ? 0 : into.hashCode());
		result = prime * result + ((selectList == null) ? 0 : selectList.hashCode());
		result = prime * result + ((where == null) ? 0 : where.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SQLSelectQueryBlock other = (SQLSelectQueryBlock) obj;
		if (parenthesized ^ other.parenthesized)
			return false;
		if (distionOption != other.distionOption)
			return false;
		if (from == null) {
			if (other.from != null)
				return false;
		} else if (!from.equals(other.from))
			return false;
		if (groupBy == null) {
			if (other.groupBy != null)
				return false;
		} else if (!groupBy.equals(other.groupBy))
			return false;
		if (into == null) {
			if (other.into != null)
				return false;
		} else if (!into.equals(other.into))
			return false;
		if (selectList == null) {
			if (other.selectList != null)
				return false;
		} else if (!selectList.equals(other.selectList))
			return false;
		if (where == null) {
			if (other.where != null)
				return false;
		} else if (!where.equals(other.where))
			return false;
		return true;
	}

}
